package com.moli.iov.base.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moli.iov.base.cache.redis.VehicleBaseCacheService;
import com.moli.iov.base.enums.QueryParamEnum;
import com.moli.iov.base.model.VehicleBaseParam;
import com.moli.iov.base.model.dto.req.VehicleBaseReqDto;
import com.moli.iov.base.model.dto.req.VehicleFenceBindingReqDto;
import com.moli.iov.base.model.po.VehicleBase;
import com.moli.iov.base.service.IVehicleBaseService;
import com.moli.iov.base.util.CollectionUtils;
import com.moli.iov.exception.BusinessException;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.base.mapper.VehicleBaseMapper;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 车辆基础信息表  服务实现类
 * </p>
 *
 * @author wanghualin
 * @since 2019-09-02
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class VehicleBaseServiceImpl extends ServiceImpl<VehicleBaseMapper, VehicleBase> implements
    IVehicleBaseService {

  @Autowired
  private VehicleBaseCacheService vehicleBaseCacheService;

  @Override
  @Transactional(rollbackFor = Exception.class)
  public RestResponse saveVehicleBase(VehicleBaseReqDto vehicleBaseReqDto) {
    log.info("VehicleBaseReqDto: " + vehicleBaseReqDto);
    if (vehicleBaseReqDto == null) {
      return RestResponse.failure("参数为空");
    }
    VehicleBase old = getVehicleBaseDbByVin(vehicleBaseReqDto.getVin());
    if (old != null) {
      return RestResponse.failure("车辆已存在");
    }
    VehicleBase vehicleBase = new VehicleBase();
    CommonUtils.copyProperties(vehicleBaseReqDto, vehicleBase);
    boolean save = this.save(vehicleBase);
    if (save) {
      vehicleBaseCacheService.setCompanyVehicleModel(vehicleBase);
    }
    return save ? RestResponse.success() : RestResponse.failure("添加失败");
  }
  private VehicleBase getVehicleBaseDbByVin(String vin) {
    QueryWrapper   queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("vin",vin);
    return this.getOne(queryWrapper, false);
  }

  @Override
  public VehicleBase getVehicleBaseByVin(String vin) {
    if (StringUtils.isBlank(vin)) {
      throw new BusinessException("vin：参数为空");
    }
    VehicleBase vehicleBaseByVin = vehicleBaseCacheService.getVehicleBaseByVin(vin);
    if (vehicleBaseByVin != null) {
      return vehicleBaseByVin;
    } else {
      VehicleBaseParam vehicleBaseParam = new VehicleBaseParam();
      vehicleBaseParam.queryEqVin(vin);
      return this.getOne(vehicleBaseParam.get(), false);
    }
  }


  @Override
  public RestResponse updateVehicleBase(VehicleBaseReqDto vehicleBaseReqDto) {
    log.info("VehicleBaseReqDto:" + vehicleBaseReqDto);
    if (vehicleBaseReqDto == null) {
      return RestResponse.failure("参数为空");
    }
    VehicleBase vehicleBase = getById(vehicleBaseReqDto.getId());
    if (vehicleBase == null) {
      return RestResponse.failure("找不到修改的车辆");
    }
    //淘汰缓存
    vehicleBaseCacheService.deleteCompanyVehicleModel(vehicleBase);
    CommonUtils.copyProperties(vehicleBaseReqDto, vehicleBase);
    boolean save = this.saveOrUpdate(vehicleBase);
    if (save) {
      vehicleBaseCacheService.setCompanyVehicleModel(vehicleBase);
    }
    return save ? RestResponse.success() : RestResponse.failure("添加失败");
  }

  @Override
  public List<VehicleBase> listVehicleBase() {
    List<VehicleBase> baseList = vehicleBaseCacheService.listVehicleBase();
    if (baseList != null && !baseList.isEmpty()) {
      return baseList;
    } else {
      VehicleBaseParam vehicleBaseParam = new VehicleBaseParam();
      List<VehicleBase> list = this.list(vehicleBaseParam.get());
      if (list != null && !list.isEmpty()) {
        vehicleBaseCacheService.saveVehicleBases(list);
      }
      return list;
    }
  }

  @Override
  public Map<String, String> listVehicleBaseMaps() {
    Map<String, String> map = vehicleBaseCacheService.listVehicleBaseMaps();
    if (map != null && !map.isEmpty()) {
      return map;
    }
    VehicleBaseParam vehicleBaseParam = new VehicleBaseParam();
    List<VehicleBase> list = this.list(vehicleBaseParam.get());
    if (list != null && !list.isEmpty()) {
      vehicleBaseCacheService.saveVehicleBases(list);
      return vehicleBaseCacheService.listVehicleBaseMaps();
    }
    return null;

  }

  @Override
  public List<VehicleBase> listVehicleBaseByCompanyId(Long companyId) {
    VehicleBaseParam vehicleBaseParam = new VehicleBaseParam();
    vehicleBaseParam.queryEqCompanyId(companyId);
    List<VehicleBase> list = this.list(vehicleBaseParam.get());
    return list;
  }

  @Override
  public List<VehicleBase> lisVehicleBaseByVin(List<String> vinList) {
    List<VehicleBase> baseList = vehicleBaseCacheService.listVehicleBaseByVin(vinList);
    if (!CollectionUtils.isEmpty(baseList)) {
      return baseList;
    }
    VehicleBaseParam vehicleBaseParam = new VehicleBaseParam();
    vehicleBaseParam.queryInVin(vinList);
    List<VehicleBase> list = this.list(vehicleBaseParam.get());
    if (!CollectionUtils.isEmpty(list)) {
      vehicleBaseCacheService.saveVehicleBases(list);
    }

    return list;
  }

  @Override
  public List<VehicleBase> listVehicleBaseNotInVinByCompanyId(Long companyId,
      List<String> vinList) {
    VehicleBaseParam vehicleBaseParam = new VehicleBaseParam();
    vehicleBaseParam.queryEqCompanyId(companyId);
    vehicleBaseParam.queryNotInVin(vinList);
    List<VehicleBase> list = this.list(vehicleBaseParam.get());
    return list;
  }

  @Override
  public void initVehicleBase() {
    //清空缓存
    vehicleBaseCacheService.cleanVehicleBaseCache();
    VehicleBaseParam vehicleBaseParam = new VehicleBaseParam();
    List<VehicleBase> list = this.list(vehicleBaseParam.get());
    if (!CollectionUtils.isEmpty(list)) {
      vehicleBaseCacheService.saveVehicleBases(list);
    }
  }

  @Override
  public VehicleBase queryVinByLicense(String license) {
    VehicleBaseParam vehicleBaseParam = new VehicleBaseParam();
    vehicleBaseParam.queryEqLicense(license);
    return getOne(vehicleBaseParam.get(), false);
  }

  @Override
  public RestResponse<List<String>> listVinByCompanyId(Long companyId) {
    List<VehicleBase> cache = vehicleBaseCacheService.listVehicleBase();
    if (!CollectionUtils.isEmpty(cache)) {
      if (companyId == null) {
        List<String> collect = cache.stream().map(VehicleBase::getVin)
                .collect(Collectors.toList());
        return RestResponse.success(collect);
      } else {

        List<String> collect = cache.stream()
                .filter(vehicleBase -> vehicleBase.getCompanyId()!=null&&
                        vehicleBase.getCompanyId().intValue() == companyId)
                .map(VehicleBase::getVin)
                .collect(Collectors.toList());
        return RestResponse.success(collect);
      }
    } else {
      List<VehicleBase> vehicleBases = listVehicleBaseByCompanyId(companyId);
      if (!CollectionUtils.isEmpty(vehicleBases)) {
        List<String> collect = vehicleBases.stream().map(VehicleBase::getVin)
                .collect(Collectors.toList());
        return RestResponse.success(collect);
      } else {
        return RestResponse.success();
      }
    }

  }

  @Override
  public List<VehicleBase> listVehicleFenceBindingList(VehicleFenceBindingReqDto dto) {
    VehicleBaseParam vehicleBaseParam = new VehicleBaseParam();
    if (dto.getQueryParam() != null && StringUtils.isNotBlank(dto.getQueryValue())) {
      if (QueryParamEnum.LICENSE.getCode() == dto.getQueryParam()) {
        vehicleBaseParam.queryLikeLeftLicense(dto.getQueryValue().trim());
      }
      if (QueryParamEnum.VIN.getCode() == dto.getQueryParam()) {
        vehicleBaseParam.queryLikeLeftVin(dto.getQueryValue().trim());
      }
    }
    vehicleBaseParam.queryNotInVin(dto.getListVin());
    vehicleBaseParam.queryEqCompanyId(dto.getCompanyId());
    return list(vehicleBaseParam.get());
  }

  @Override
  public List<VehicleBase> listVehicleBaseInVinByCompanyId(Long companyId, List<String> listVin) {
    VehicleBaseParam vehicleBaseParam = new VehicleBaseParam();
    vehicleBaseParam.queryInVin(listVin);
    vehicleBaseParam.queryEqCompanyId(companyId);
    return list(vehicleBaseParam.get());
  }

  @Override
  public List<VehicleBase> listVehicleFenceUnBindingList(VehicleFenceBindingReqDto dto) {
    VehicleBaseParam vehicleBaseParam = new VehicleBaseParam();
    if (dto.getQueryParam() != null && StringUtils.isNotBlank(dto.getQueryValue())) {
      if (QueryParamEnum.LICENSE.getCode() == dto.getQueryParam()) {
        vehicleBaseParam.queryLikeLeftLicense(dto.getQueryValue().trim());
      }
      if (QueryParamEnum.VIN.getCode() == dto.getQueryParam()) {
        vehicleBaseParam.queryLikeLeftVin(dto.getQueryValue().trim());
      }
    }
    vehicleBaseParam.queryInVin(dto.getListVin());
    return list(vehicleBaseParam.get());
  }

  @Override
  public VehicleBase getVehicleBaseUnauthorizedByVin(String vin) {
     return getVehicleBaseDbByVin(vin);
  }


}
